package com.yiren.dbaa.module.device.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.CarSignalConfigBean;
import com.yiren.backstage.config.bean.device.DeviceStudyConfigBean;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.enums.CarSignalEnum;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.contract.DeviceConfigContract;
import com.yiren.dbaa.module.device.contract.LowerComputerContract;
import com.yiren.dbaa.module.device.model.DeviceConfigModel;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;
import com.yiren.dbaa.net.bean.WiringConfigBean;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lwb
 * @date : 2022/7/14 15:35
 * @description: 设备配置P
 */
public class DeviceConfigPresenter implements DeviceConfigContract.Presenter {

    private Activity activity;
    private DeviceConfigContract.Model deviceConfigModel;
    private LowerComputerPresenter lowerComputerPresenter;
    private List<WiringConfigBean> wiringConfigList;
    private Gson gs = new Gson();
    private Map<String, String> map = new HashMap<String, String>();

    public DeviceConfigPresenter(Activity activity) {
        this.activity = activity;
        deviceConfigModel = new DeviceConfigModel(activity);
        lowerComputerPresenter = new LowerComputerPresenter(activity);
    }

    @Override
    public String getObdName() {
        if (G.connectDeviceMessage != null) {
            if (G.connectDeviceMessage.getVersionDescribe().contains("MP")) {
                return lowerComputerPresenter.getObdCarModelName();
            } else {
                return "接线板";
            }
        } else {
            return "未连接";
        }
    }

    @Override
    public void switchObd(LowerComputerContract.View view) {
        lowerComputerPresenter.switchObdCarModel(view);
    }

    @Override
    public void switchObdCarModel(int obdCarModelCode) {
        lowerComputerPresenter.switchObdCarModel(obdCarModelCode);
    }

    @Override
    public void loadWiringConfig(DeviceConfigContract.View view) {
        // 判断有没有配置过的信号，有则不显示
        String pwd = LoadConfigApi.loadDeviceStudyConfig(activity).getJxConfig();
        wiringConfigList = new ArrayList<>();
        if (StringUtils.isNotEmpty(pwd)) {
            map = gs.fromJson(pwd, map.getClass());
            Set<String> lineNames = map.keySet();
            setWiringConfig(lineNames, "棕线");
            setWiringConfig(lineNames, "灰线");
            setWiringConfig(lineNames, "绿线");
            setWiringConfig(lineNames, "黄线");
            setWiringConfig(lineNames, "蓝线");
            setWiringConfig(lineNames, "紫线");
            setWiringConfig(lineNames, "橙线");
            setWiringConfig(lineNames, "粉线");
            if (null != G.connectDeviceMessage && (G.connectDeviceMessage.getVersionDescribe().contains("YR") || G.connectDeviceMessage.getVersionDescribe().contains("XQ") || G.connectDeviceMessage.getVersionDescribe().contains("WP"))) {
                setWiringConfig(lineNames, "白线");
                setWiringConfig(lineNames, "黑线");
            } else {
                map.remove("白线");
                map.remove("黑线");
                DeviceStudyConfigBean deviceStudyConfig = LoadConfigApi.loadDeviceStudyConfig(activity);
                deviceStudyConfig.setJxConfig(gs.toJson(map));
                ParamConfigApi.saveDeviceStudyConfig(activity, deviceStudyConfig);
            }
        } else {
            wiringConfigList.add(new WiringConfigBean("棕线", null, 0));
            wiringConfigList.add(new WiringConfigBean("灰线", null, 0));
            wiringConfigList.add(new WiringConfigBean("绿线", null, 0));
            wiringConfigList.add(new WiringConfigBean("黄线", null, 0));
            wiringConfigList.add(new WiringConfigBean("蓝线", null, 0));
            wiringConfigList.add(new WiringConfigBean("紫线", null, 0));
            wiringConfigList.add(new WiringConfigBean("橙线", null, 0));
            wiringConfigList.add(new WiringConfigBean("粉线", null, 0));
            if (null != G.connectDeviceMessage && (G.connectDeviceMessage.getVersionDescribe().contains("YR") || G.connectDeviceMessage.getVersionDescribe().contains("XQ") || G.connectDeviceMessage.getVersionDescribe().contains("WP"))) {
                wiringConfigList.add(new WiringConfigBean("白线", null, 0));
                wiringConfigList.add(new WiringConfigBean("黑线", null, 0));
            }
        }
        view.showWiringConfigList(wiringConfigList);
    }

    /**
     * 设置接线配置list
     */
    private void setWiringConfig(Set<String> lineNames, String lineName) {
        if (lineNames.contains(lineName)) {
            wiringConfigList.add(new WiringConfigBean(lineName, CarSignalEnum.valueOf(map.get(lineName)), 1));
        } else {
            wiringConfigList.add(new WiringConfigBean(lineName, null, 0));
        }
    }

    @Override
    public void showSelectStartProject(int position, DeviceConfigContract.View view) {
        if (wiringConfigList.get(position).getAction() == 0) {
            List<CarSignalEnum> signalLists = deviceConfigModel.setDialogContent(map);
            deviceConfigModel.showSelectSignalDialog(new CommonCallBack() {
                @Override
                public void success(Object result) {
                    int signalPosition = (int) result;
                    wiringConfigList.get(position).setCarSignalEnum(signalLists.get(signalPosition));
                    wiringConfigList.get(position).setAction(1);
                    view.refreshAdapter();
                    map.put(wiringConfigList.get(position).getLineName(), signalLists.get(signalPosition).getCode());
                    DeviceStudyConfigBean deviceStudyConfig = LoadConfigApi.loadDeviceStudyConfig(activity);
                    deviceStudyConfig.setJxConfig(gs.toJson(map));
                    ParamConfigApi.saveDeviceStudyConfig(activity, deviceStudyConfig);
                }
            }, wiringConfigList, position);
        } else {
            map.remove(wiringConfigList.get(position).getLineName());
            wiringConfigList.get(position).setCarSignalEnum(null);
            wiringConfigList.get(position).setAction(0);
            view.refreshAdapter();
            DeviceStudyConfigBean deviceStudyConfig = LoadConfigApi.loadDeviceStudyConfig(activity);
            deviceStudyConfig.setJxConfig(gs.toJson(map));
            ParamConfigApi.saveDeviceStudyConfig(activity, deviceStudyConfig);
        }

    }

    @Override
    public void saveCorrectNumber(String speed, String rotateSpeed) {
        CarStateBean carState = CarStateBean.getInstance();
        CarSignalConfigBean carSignalConfigBean = LoadConfigApi.loadCarSignalConfig(activity);
        float mc = carState.getSpeedPulse();
        if (mc != 0) {
            int value = Integer.parseInt(speed);
            float cxs = new BigDecimal(Float.valueOf(value * 1000 / (mc * 72))).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
            carSignalConfigBean.getMap().put(CarStateNameEnum.SPEED.toString(), cxs);
        }
        float mc_r = carState.getRotateSpeedPulse();
        if (mc_r != 0) {
            int value = Integer.parseInt(rotateSpeed);
            float zxs = new BigDecimal(Float.valueOf(value / mc)).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
            carSignalConfigBean.getMap().put(CarStateNameEnum.ROTATE_SPEED.toString(), zxs);
        }
        ParamConfigApi.saveCarSignalConfig(activity, carSignalConfigBean);
    }

    @Override
    public void setCarStateSignal(String carStateSignal) {
        deviceConfigModel.setCarStateSignal(carStateSignal, new CommonCallBack() {
            @Override
            public void success(Object result) {

            }
        });

    }


}
